package com.example.lib.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import android.widget.AdapterView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatSpinner
import androidx.core.content.res.ResourcesCompat
import com.example.lib.R
import com.example.lib.extend.dip2px
import com.example.lib.listener.AutoSpinnerItemSelectorListener

class AutoSpinner : AppCompatSpinner {
    private var lastPosition = 0
    private var firstSelect = true
    private var listener: AutoSpinnerItemSelectorListener? = null
    private var isWrap = false

    constructor(context: Context, mode: Int) : super(context, mode) {
        init(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, mode: Int) : super(context,
        attrs,
        defStyleAttr,
        mode) {
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.AutoSpinner)
        val color = typeArray.getColor(R.styleable.AutoSpinner_spinnerTextColor, Color.WHITE)
        val image =
            typeArray.getResourceId(R.styleable.AutoSpinner_spinnerDropdownImage, R.mipmap.icon_white_arrow_down)
        isWrap = typeArray.getBoolean(R.styleable.AutoSpinner_spinnerDropdownWidthWrapContent, false)
        typeArray.recycle()

        if (isWrap) {
            post {
                dropDownWidth = width
            }
        }

        onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                if (parent.childCount > 0) {
                    (parent.getChildAt(0) as TextView).setTextColor(color)
                    (parent.getChildAt(0) as TextView).setTypeface(null, Typeface.BOLD)
                    (parent.getChildAt(0) as TextView).compoundDrawablePadding = context.dip2px(6f)
                    (parent.getChildAt(0) as TextView).setCompoundDrawablesWithIntrinsicBounds(null,
                        null,
                        ResourcesCompat.getDrawable(resources, image, null),
                        null)
                }
                if (firstSelect) {
                    firstSelect = false
                    return
                }

                listener?.onItemSelect(position)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }
    }

    override fun setSelection(position: Int, animate: Boolean) {
        if (position == lastPosition) {
            return
        }
        lastPosition = position
        super.setSelection(position, animate)
    }

    override fun setVisibility(visibility: Int) {
        super.setVisibility(visibility)
        if (visibility == VISIBLE) {
            if (isWrap) {
                post {
                    dropDownWidth = width
                }
            }
        }
    }

    override fun setSelection(position: Int) {
        if (position == lastPosition) {
            return
        }
        lastPosition = position
        super.setSelection(position)
    }

    fun setItemSelectListener(listener: AutoSpinnerItemSelectorListener?) {
        this.listener = listener
    }
}